<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator="/" style="padding-left: 10px;padding-bottom: 10px;font-size: 12px">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">系统管理</a></el-breadcrumb-item>
      <el-breadcrumb-item><a >用户管理</a></el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片-->
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="部门" label-width="70px">
          <template>
            <el-select v-model="formInline.city" placeholder="请选择"clearable >
              <el-option
                v-for="item in departments"
                :key="item.id"
                :label="item.name"
                :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">
                  <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{ item.deptCount }}</span>
                </span>
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="用户名" label-width="70px">
          <el-input v-model="formInline.user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 10px">
          <el-button icon="el-icon-refresh" v-on:click="clearName">重置</el-button>
          <el-button type="primary" icon="el-icon-search" v-on:click="queryByUserName">查询</el-button>
          <el-button type="success" icon="el-icon-plus" v-on:click="dialogVisible=true">添加</el-button>
          <el-button type="warning" icon="el-icon-download" ><a href=" http://localhost:8090/user/exportUserToExcel">导出</a></el-button>
        </el-form-item>
        <el-form-item prop="file">
          <el-upload
            class="upload-demo"
            ref="upload"
            multiple
            accept=".xls"
            action=""
            :http-request="httpRequest"
            :file-list="fileList"
            :auto-upload="false"
          >
            <el-button slot="trigger"  type="primary">选取文件</el-button>
            <el-button style="margin-left:5px" type="danger" icon="el-icon-upload"  v-on:click="submitUploadList">导入</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <!--表格内容显示区域-->
      <el-table
        :data="userList"
        border
        max-height="400px"
        style="width: 100%;height: 400px;">
        <el-table-column
          prop="id"
          label="编号"
          width="50">
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="110">
        </el-table-column>
        <el-table-column
          prop="sex"
          label="性别"
          width="60">
          <template slot-scope="scope">
            {{scope.row.sex == 0 ? '男':(scope.row.sex == 1 ? '女' : '保密')}}
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="所属部门"
          width="180">
        </el-table-column>
        <el-table-column
          sortable
          prop="birth"
          label="生日"
          width="180">
        </el-table-column>
        <el-table-column
          sortable
          prop="email"
          label="邮箱"
          width="180">
        </el-table-column>
        <el-table-column
          prop="phoneNumber"
          label="电话"
          width="150">
        </el-table-column>
        <el-table-column
          prop="status"
          label="是否禁用"
          width="100">
          <template slot-scope="scope">
            <el-switch
              v-on:change="changeStatus(scope.row.status,scope.row.id)"
              v-model="scope.row.status==1"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column
          label="操作">
          <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit"  v-on:click="queryUserById(scope.row)">修改</el-button>
           <el-button type="danger" size="mini" icon="el-icon-delete" v-on:click="deleteUser(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="padding-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 12, 24, 36]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--添加弹窗-->
    <el-dialog title="添加" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="userForm" label-width="80px">
        <el-form-item label="用户名:">
          <el-input v-model="userForm.username"
                    placeholder="登录账号"></el-input>
        </el-form-item>
        <el-form-item label="昵称:">
          <el-input v-model="userForm.nickname" placeholder="个性昵称"></el-input>
        </el-form-item>
        <el-form-item label="邮箱:">
          <el-input v-model="userForm.email" placeholder="例如:xxx@XX.com"></el-input>
        </el-form-item>
        <el-form-item label="联系电话:">
          <el-input v-model="userForm.phoneNumber" placeholder="11位数字"></el-input>
        </el-form-item>


        <el-form-item label="状态:">
          <template >
            <el-radio-group v-model="updateUserList.status">
              <el-radio :label="0">不可用</el-radio>
              <el-radio :label="1">可用</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item label="创建时间:">
          <div>
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="userForm.createTime"
              type="datetime"
              style="width:100%;"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions1">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="修改时间:">
        <div>
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="userForm.modifiedTime"
            type="datetime"
            style="width:100%;"
            placeholder="选择日期时间"
            align="right"
            :picker-options="pickerOptions1">
          </el-date-picker>
        </div>
      </el-form-item>
        <el-form-item label="性别:">
          <template >
            <el-radio-group v-model="userForm.sex">
              <el-radio :label="0">男</el-radio>
              <el-radio :label="1">女</el-radio>
              <el-radio :label="2">保密</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>

        <el-form-item label="类型:">
          <template >
            <el-radio-group v-model="userForm.type">
              <el-radio :label="0">超级管理员</el-radio>
              <el-radio :label="1">系统用户</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
        <el-form-item label="生日:">
          <div>
            <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="userForm.birth"
              type="date"
              style="width:100%;"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions1">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="是否删除:" style="display:none;">
          <el-input v-model="userForm.deleted" ></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="userForm" class="demo-form-inline"   style="width:464px;">
        <el-form-item label="部门:">
          <template >
            <el-select v-model="userForm.departmentId"  placeholder="请选择" clearable  style="width:334px;" >
              <el-option
                v-for="item in departments"
                :key="item.id"
                :label="item.name"
                :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">
                  <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{ item.deptCount }}</span>
                </span>
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!--修改弹窗-->
    <el-dialog title="修改" :visible.sync="dialogUser" width="30%">
      <el-form ref="form" :model="updateUserList" label-width="80px">
        <el-form-item label="用户名:">
          <el-input v-model="updateUserList.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱:">
          <el-input v-model="updateUserList.email"></el-input>
        </el-form-item>
        <el-form-item label="联系电话:">
          <el-input v-model="updateUserList.phoneNumber"></el-input>
        </el-form-item>
        <el-form-item label="性别:">
          <template >
            <el-radio-group v-model="updateUserList.sex">
              <el-radio :label="0">男</el-radio>
              <el-radio :label="1">女</el-radio>
              <el-radio :label="2">保密</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="updateUserList" class="demo-form-inline"   style="width:464px;">
        <el-form-item label="部门:">
          <template >
            <el-select v-model="updateUserList.departmentId"  clearable  style="width:334px;" >
              <el-option
                v-for="item in departments"
                :key="item.id"
                :label="item.name"
                :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">
                  <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{ item.deptCount }}</span>
                </span>
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { queryDeptHasCount } from '../../api/dept'
  import { updateUserById,queryAllUserByUserName,deleteUserById} from "../../api/users";



  export default {
    name: 'User',
    data () {
      return {
        pageSize: 6,
        total: 1,
        current: 1,
        currentPage4: 4,
        formInline: {
          user: '',
          email: '',
          city: '',
          radio: '1',
        },
        userForm:{
          password:'888888',
        },
        updateUserList:{
          deleted:0,
        },
        fileList: [],
        loadUrl:'user/importUserToExcel',
        departments: [],
        dialogVisible:false,
        dialogUser:false,
        userList: null,
        pickerOptions1: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      }
    },
    methods: {
      // 提交事件
      onSubmit () {
        console.log('submit!')
      },
      //导入数据
      httpRequest(param) {
        console.log(param);
        let fileObj = param.file; // 相当于input里取得的files
        let fd = new FormData(); // FormData 对象
        fd.append("file", fileObj); // 文件对象
        let url = this.loadUrl
        let config = {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        };
        this.$http.post(url, fd, config).then((res) => {
          console.log(res);
        });
      },
      submitUploadList() {
        this.$refs.upload.submit();
        this.$message({
          type: "success",
          message: "导入成功",
        });
      },
      // 当记录数发生改变时
      handleSizeChange (val) {
        this.pageSize = val;
        let departmentId=this.formInline.city;
        let username=this.formInline.user;
        this.getUserList(this.current,this.pageSize,departmentId,username);
      },
      // 当前页发生改变时
      handleCurrentChange (val) {
        this.current = val;
        let departmentId=this.formInline.city;
        let username=this.formInline.user;
        this.getUserList(this.current,this.pageSize,departmentId,username);
      },
      // 获取用户数据
      async getUserList () {
        let departmentId=this.formInline.city;
        let username=this.formInline.user;
        const { data } = await queryAllUserByUserName(this.current,this.pageSize,departmentId,username);
        this.userList = data.data.userPage.records;
        this.total = data.data.userPage.total;
        this.pageSize = data.data.userPage.size;
      },
      // 获取部门数据
      async getDeptHasCount () {
        const { data } = await queryDeptHasCount()
        this.departments = data.data.departments;
      },
      //修改用户状态
      async changeStatus(status,id){
        let departmentId=this.formInline.city;
        let username=this.formInline.user;
        if(status===1){
          status=0;
          await updateUserById(status,id);
          this.$message({
            type: "info",
            message: "禁用成功",
          });
          this.getUserList(this.current,this.pageSize,departmentId,username);
          return ;
        }else{
          status=1;
          await updateUserById(status,id);
          this.$message({
            type: "success",
            message: "启用成功",
          });
          this.getUserList(this.current,this.pageSize,departmentId,username);
          return ;
        }
      },
      //修改用户信息
    async updateUser(){
      let departmentId=this.formInline.city;
      let username=this.formInline.user;
      let url='user/updateUserById'
      this.$http.post(url, this.updateUserList).then((res) => {
        console.log(res);
        this.getUserList(this.current,this.pageSize,departmentId,username);
        this.$message({
          type: "info",
          message: "保存成功",
        });
        this.dialogUser = false;
        this.updateUserList.departmentId='';
      }).catch(ex=>
        this.$message({
          type: "error",
          message: "保存失败",
        }));
    },
      //添加用户信息
      async addUser(){
        let departmentId=this.formInline.city;
        let username=this.formInline.user;
        let url='user/insertUser'
        this.$http.post(url, this.userForm).then((res) => {
          this.getUserList(this.current,this.pageSize,departmentId,username);
          console.log(res);
          this.userForm={};
          this.$message({
            type: "info",
            message: "保存成功",
          });
          this.dialogVisible = false;
        }).catch(ex=>{
          this.$message({
            type: "error",
            message: "保存失败",
          });
        });
      },
      //删除用户信息
      async deleteUser(id){
        let departmentId=this.formInline.city;
        let username=this.formInline.user;
        await deleteUserById(id);
        this.$message({
          type: "info",
          message: "删除成功",
        });
        this.getUserList(this.current,this.pageSize,departmentId,username);
      },
      //获取当前选中用户信息
      async queryUserById(row){
        this.updateUserList=row;
        this.dialogUser=true;
      },
      //模糊查询用户信息
      async queryByUserName(){
        let departmentId=this.formInline.city;
        console.log(departmentId)
        let username=this.formInline.user;
        const { data } =  await queryAllUserByUserName(1,this.pageSize,departmentId,username);
        this.userList = data.data.userPage.records;
        this.total = data.data.userPage.total;
        this.pageSize = data.data.userPage.size;
      },
      //重置模糊查询条件
      async clearName(){
       let departmentId=  this.formInline.city='';
       let username=  this.formInline.user='';
        this.getUserList(this.current,this.pageSize,departmentId,username);
      }
    },
    // create创建完成后钩子函数
    created () {
      this.getUserList();
      this.getDeptHasCount();
    }
  }
</script>
<style scoped>
  a {
    text-decoration: none;
    color:white;
  }
  /*正常的未被访问过的链接*/
  a:link {
    text-decoration: none;
  }
  /*已经访问过的链接*/
  a:visited {
    text-decoration: none;
  }
  /*鼠标划过(停留)的链接*/
  a:hover {
    text-decoration: none;
  }
  /* 正在点击的链接，鼠标在元素上按下还没有松开*/
  a:active {
    text-decoration: none;
  }
  /* 获得焦点的时候 鼠标松开时显示的颜色*/
  a:focus {
    text-decoration: none;
  }
</style>
